package com.wavefull.core.ui.toolbar

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.marginTop
import com.wavefull.core.R
import com.wavefull.core.databinding.TitleBarLayoutBinding
import com.wavefull.core.ext.*

class TitleBar : LinearLayout {

    private lateinit var binding: TitleBarLayoutBinding
    private var onBackClickListener: OnBackClickListener? = null

    constructor(context: Context) : super(context) {
        initTitleBar(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initTitleBar(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initTitleBar(context)
    }

    private fun initTitleBar(context: Context) {
        binding = TitleBarLayoutBinding.inflate(LayoutInflater.from(context), null, false)
//        val inflate = LayoutInflater.from(context).inflate(R.layout.title_bar_layout, null, false)
        addView(binding.root)
        val linearLayout = findViewById<RelativeLayout>(R.id.linear)

        linearLayout.setLayoutParams(
            LinearLayoutCompat.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                context.getActionBarSize()
            )
        )

        linearLayout.getSystemWindow { systemWindow ->
            //为需要偏移的view设置padding
            linearLayout.setPadding(0, systemWindow.top, 0, 0)
            linearLayout.setLayoutParams(
                LinearLayoutCompat.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    context.getActionBarSize() + systemWindow.top
                )
            )
        }

        binding.layoutBack.setOnClickListener {
            if (onBackClickListener != null) {
                onBackClickListener!!.onBackClicked()
            }
        }
    }

    fun setOnBackClickListener(onBackClickListener: OnBackClickListener?): TitleBar {
        this.onBackClickListener = onBackClickListener
        return this
    }

    interface OnBackClickListener {
        fun onBackClicked()
    }

    /**
     * 描述：设置标题文本.
     *
     * @param text 文本
     */
    fun setTitleText(text: String): TitleBar {
        binding.titleView.text = text
        return this
    }

    fun setLeftImg(b: Boolean): TitleBar {
        binding.backView.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun getTvComplete(): LinearLayout {
        binding.tvCompleteLayout.visibility = VISIBLE
        return binding.tvCompleteLayout
    }
}
